Skip to main content

Overview

The CoCa (Contrastive Captioner) class implements a multimodal model that combines contrastive learning (like CLIP) with generative captioning capabilities. It consists of:
  1. Image encoder - Encodes images into a latent space and produces token embeddings
  2. Text encoder - Encodes text into contrastive features for CLIP-style learning
  3. Multimodal text decoder - Generates captions conditioned on image embeddings
CoCa can be used for both zero-shot image-text matching and image captioning.

Class Definition

Initialization Parameters

int
required
Dimensionality of the joint embedding space for contrastive image and text features.
MultimodalCfg
required
Configuration for the multimodal text decoder. Controls the cross-attention layers that condition text generation on image features.
CLIPTextCfg
required
Configuration for the unimodal text encoder used for contrastive learning.
CLIPVisionCfg
required
Configuration for the vision encoder.
bool
default:"False"
Use QuickGELU activation instead of standard GELU.
float
default:"np.log(1 / 0.07)"
Initial value for the learned temperature parameter in contrastive learning.
Optional[float]
default:"None"
Optional learnable bias term added to contrastive logits.
bool
default:"False"
If True, logit_scale has shape [1] instead of [].
Optional[torch.dtype]
default:"None"
Precision for model computations (e.g., torch.float16, torch.bfloat16).
int
default:"0"
Token ID used for padding in the vocabulary.

Attributes

  • visual: Vision encoder module
  • text: Unimodal text encoder for contrastive learning
  • text_decoder: Multimodal transformer decoder for caption generation
  • logit_scale: Learned temperature parameter for contrastive learning
  • logit_bias: Optional learned bias for contrastive logits
  • pad_id: Padding token ID
  • context_length: Maximum sequence length for caption generation

Key Methods

encode_image

Encodes images into the contrastive embedding space. Parameters:
  • images: Image tensor of shape (batch_size, channels, height, width)
  • normalize: If True, L2-normalizes the output features
Returns: Image features of shape (batch_size, embed_dim)

encode_text

Encodes tokenized text into the contrastive embedding space. Parameters:
  • text: Tokenized text tensor of shape (batch_size, context_length)
  • normalize: If True, L2-normalizes the output features
Returns: Text features of shape (batch_size, embed_dim)

forward

Forward pass through the model. Parameters:
  • image: Image tensor of shape (batch_size, channels, height, width)
  • text: Optional tokenized text for teacher-forcing caption generation
  • image_latent: Optional pre-computed contrastive image features
  • image_embs: Optional pre-computed image token embeddings
  • output_labels: If True, creates caption labels by shifting text input
Returns: Dictionary containing:
  • image_features: Contrastive image embeddings (batch_size, embed_dim)
  • text_features: Contrastive text embeddings (batch_size, embed_dim) (if text provided)
  • logits: Caption generation logits (batch_size, seq_len, vocab_size) (if text provided)
  • labels: Ground truth labels for caption loss (batch_size, seq_len-1) (if output_labels=True)
  • logit_scale: Exponential of learned temperature parameter
  • logit_bias: Learned bias (if initialized with init_logit_bias)
  • image_embs: Image token embeddings (if text not provided)

generate

Generates captions for images using beam search or sampling strategies. Parameters:
  • image: Image tensor to caption
  • text: Optional text prefix to continue from
  • seq_len: Target sequence length for generation
  • max_seq_len: Maximum context length (default 77)
  • temperature: Sampling temperature (higher = more random)
  • generation_type: One of “beam_search”, “top_p”, or “top_k”
  • top_p: Nucleus sampling parameter (keep tokens in top-p probability mass)
  • top_k: Top-k sampling parameter (keep top k tokens)
  • pad_token_id: Padding token (default 0)
  • eos_token_id: End-of-sequence token (default 49407)
  • sot_token_id: Start-of-text token (default 49406)
  • num_beams: Number of beams for beam search
  • num_beam_groups: Number of beam groups for diverse beam search
  • min_seq_len: Minimum generated sequence length
  • stopping_criteria: Optional list of stopping criteria
  • repetition_penalty: Penalty for repeated tokens (1.0 = no penalty)
  • fixed_output_length: If True, pad output to seq_len
Returns: Generated token IDs of shape (batch_size, seq_len) Note: Requires transformers library: pip install transformers

set_grad_checkpointing

Enables gradient checkpointing for all three model components (visual, text, text_decoder) to reduce memory usage.

Usage Example

Contrastive Learning Example

Caption Generation with Custom Parameters